package service

import (
	"strconv"
	"time"
	"zg3/config"
	"zg3/internal/api/request"
	"zg3/internal/models/dao"
)

func ListVideo(video *request.ListVideo) []*dao.Video {
	var videos []*dao.Video
	config.DB.Model(&dao.Video{}).Order("likes DESC").Order("look DESC").Scopes(Paginate(video.Page, video.PageSize)).Find(&videos)
	return videos
}

func BeginVideo(video *request.BeginVideo, userId int) (string, string) {
	var videos *dao.Video
	err := config.DB.Where("id=?", video.VideoId).Limit(1).Find(&videos).Error
	if err != nil {
		return "", "播放失败"
	}
	err = config.DB.Model(&dao.Video{}).Where("id=?", video.VideoId).Update("look", videos.Look+1).Error
	if err != nil {
		return "", "播放失败"
	}

	var ILook *dao.Video
	config.DB.Model(&dao.Video{}).Where("id=?", video.VideoId).Find(&ILook)

	day := time.Now().Day()

	Num := "look_day_" + strconv.Itoa(day) + "_" + strconv.Itoa(video.VideoId)

	config.Rdb.Set(config.Ctx, Num, ILook.Look, time.Hour*24)

	return videos.File, ""
}

func VideoLike(like *request.VideoLike, userId int) (int, string) {
	var have *dao.VideoLike
	var videos *dao.Video
	var ILike *dao.Video
	config.DB.Where("id=?", like.VideoId).Limit(1).Find(&videos)
	config.DB.Where("video_id=? and user_id=?", like.VideoId, userId).Find(&have)
	day := time.Now().Day()

	config.DB.Model(&dao.Video{}).Where("id=?", like.VideoId).Find(&ILike)

	Num := "like_day_" + strconv.Itoa(day) + "_" + strconv.Itoa(like.VideoId)

	config.Rdb.Set(config.Ctx, Num, ILike.Likes, time.Hour*24)
	if have.ID != 0 {
		config.DB.Model(&dao.Video{}).Where("id=?", like.VideoId).Update("likes", videos.Likes+1)
		return int(have.ID), ""
	}
	video := dao.VideoLike{
		UserId:  userId,
		VideoId: like.VideoId,
	}
	err := config.DB.Model(&dao.VideoLike{}).Create(&video).Error
	if err != nil {
		return 0, "点赞失败"
	}

	err = config.DB.Model(&dao.Video{}).Where("id=?", like.VideoId).Update("likes", videos.Likes+1).Error
	if err != nil {
		return 0, "点赞失败"
	}

	return int(video.ID), ""
}

func FindLikeVideo(userId int) []*dao.Video {
	var like []*dao.VideoLike
	var videos []*dao.Video
	config.DB.Where("user_id=?", userId).Find(&like)
	for _, v := range like {
		var one *dao.Video
		config.DB.Where("id=?", v.VideoId).Find(&one)
		videos = append(videos, one)
	}
	return videos
}

func NoLike(like *request.NoLike, userId int) string {
	err := config.DB.Model(&dao.VideoLike{}).Where("user_id=? and video_id=?", userId, like.VideoId).Delete(&dao.VideoLike{}).Error
	if err != nil {
		return "取消点赞失败"
	}
	return ""
}

func GetTopVideo() {
	day := time.Now().Day()
	var sli []*dao.Video
	config.DB.Find(&sli)
	for _, v := range sli {
		LookNum := "look_day_" + strconv.Itoa(day) + "_" + strconv.Itoa(int(v.ID))
		LikeNum := "like_day_" + strconv.Itoa(day) + "_" + strconv.Itoa(int(v.ID))
		val := config.Rdb.Get(config.Ctx, LookNum).Val()
		look := dao.VideoTopLooks{
			VideoId: int(v.ID),
			Looks:   val,
		}
		config.DB.Create(&look)
		s := config.Rdb.Get(config.Ctx, LikeNum).Val()
		likes := dao.VideoTopLikes{
			VideoId: int(v.ID),
			Likes:   s,
		}
		config.DB.Create(&likes)
	}
}

func FindTopVideo() ([]*dao.VideoTopLooks, []*dao.VideoTopLikes, string) {
	var look []*dao.VideoTopLooks
	err := config.DB.Order("looks DESC").Limit(10).Find(&look).Error
	if err != nil {
		return nil, nil, "获取失败"
	}
	var likes []*dao.VideoTopLikes
	err = config.DB.Order("likes DESC").Limit(10).Find(&likes).Error
	if err != nil {
		return nil, nil, "获取失败"
	}
	return look, likes, ""
}
